package com.tao.auto.batchtao.home;

import com.tao.auto.batchtao.base.ParamContext;
import com.tao.auto.batchtao.base.PhoneMessageCode;
import com.tao.auto.batchtao.base.accout.BaseAccount;
import com.tao.auto.batchtao.database.dao.MgtUserInfoRepository;
import com.tao.auto.batchtao.database.entity.MgtUserInfo;
import com.tao.auto.batchtao.home.account.HomeAccount;
import com.tao.auto.batchtao.home.account.HomeAccountQueue;
import com.tao.auto.batchtao.home.account.Sign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Example;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author GLNC-taowenchen
 */
@Component
public class HomeRegisterBaseFlowImpl extends AbstractHomeRegisterBaseFlow {

    private PhoneMessageCode<String> deafultPhoneMessageCode;
    private BaseAccount<HomeAccount> homeAccount;
    private RestTemplate restTemplate;
    private MgtUserInfoRepository mgtUserInfoRepository;

    public HomeRegisterBaseFlowImpl(@Qualifier("homeRegisterAccountImpl") BaseAccount<HomeAccount> homeAccount
            , RestTemplate restTemplate
            , MgtUserInfoRepository mgtUserInfoRepository
            , PhoneMessageCode<String> deafultPhoneMessageCode) {
        this.homeAccount = homeAccount;
        this.restTemplate = restTemplate;
        this.deafultPhoneMessageCode = deafultPhoneMessageCode;
        this.mgtUserInfoRepository = mgtUserInfoRepository;
    }

    @Override
    protected ParamContext<HomeAccount> getAccount(String userName) {
        if (StringUtils.isBlank(userName)) {
            return new ParamContext<HomeAccount>().paramContext(homeAccount.account());
        } else {
            HomeAccount homeAccount = new HomeAccount();
            homeAccount.setAccount(userName);
            homeAccount.setNickname(userName + "aaa");
            homeAccount.setPassword(userName + "123");
            homeAccount.setSpread("733");
            homeAccount.setSign("0");
            return new ParamContext<HomeAccount>().paramContext(homeAccount);
        }

    }


    @Override
    protected ParamContext<String> getSign() {
        ResponseEntity<Sign> forEntity = restTemplate.getForEntity("http://cr.dongweicom.com/api/verify_code", Sign.class);
        return new ParamContext<String>().paramContext(forEntity.getBody().getData().getKey());
    }

    @Override
    protected void sendCode(ParamContext<HomeAccount> phone, ParamContext<String> sign) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = "http://cr.dongweicom.com/api/register/verify";
        Map<String, String> params = new HashMap<>(8);
        params.put("phone", phone.getData().getAccount());
        params.put("type", "register");
        params.put("key", sign.getData());
        params.put("code", "");
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        System.out.println(stringResponseEntity);
    }

    @Override
    protected ParamContext<String> getCode(ParamContext<HomeAccount> phone) {
        String code = deafultPhoneMessageCode.code(phone.getData().getAccount());
        code = code.substring(code.indexOf("您的验证码是：") + 7, code.indexOf("您的验证码是：") + 13);
        return new ParamContext<String>().paramContext(code);
    }

    @Override
    protected void register(ParamContext<HomeAccount> phone, ParamContext<String> sign, ParamContext<String> code) {
        if (code.getData() == null) {
            return;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = "http://cr.dongweicom.com/api/register";
        Map<String, String> params = new HashMap<>(8);
        params.put("account", phone.getData().getAccount());
        params.put("nickname", phone.getData().getNickname());
        params.put("captcha", code.getData());
        params.put("password", phone.getData().getPassword());
        params.put("spread", phone.getData().getSpread());
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        System.out.println(stringResponseEntity.getBody());
    }


    @Override
    protected void save(ParamContext<HomeAccount> phone) {
        System.out.println(phone.getData().getAccount());
        Optional<MgtUserInfo> mgtUserInfoByUsername = mgtUserInfoRepository.findMgtUserInfoByUsername(phone.getData().getAccount());
        MgtUserInfo mgtUserInfo = new MgtUserInfo();
        if (!mgtUserInfoByUsername.isPresent()) {
            //更新数据库
            mgtUserInfo.setUsername(phone.getData().getAccount());
            mgtUserInfo.setPassword(phone.getData().getPassword());
            mgtUserInfo.setNumber(0L);
            mgtUserInfo.setInvitationCode(Long.parseLong(phone.getData().getSpread()));
            mgtUserInfo.setFatherInvitationCode(Long.parseLong(phone.getData().getSpread()));
            mgtUserInfo.setSign(phone.getData().getSign());
            mgtUserInfo.setSignTime("0");

        } else {
            mgtUserInfo = mgtUserInfoByUsername.get();
        }
        mgtUserInfo.setRegisterTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        mgtUserInfoRepository.save(mgtUserInfo);
        try {
            HomeAccountQueue.queue.put(mgtUserInfo);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
